<?php
// $Id$
/**
 * @file
 * Administration page callbacks for the druais module.
 */

/**
 * Form builder. Configure druais
 * 
 * @ingroup forms
 * @see system_settings_form().
 */
 function druais_admin_settings() {
   $form['druais_numberof_class'] = array(
     '#type' => 'textfield',
     '#size' => 3,
     '#title' => 'Add some class to you school',
     '#default_value' => variable_get('druais_numberof_class', 3),
     '#description' => t('Define how many class in your school')
   );
   
   $form['druais_numberof_group'] = array(
     '#type' => 'textfield',
     '#size' => 3,
     '#title' => 'How many group per class',
     '#default_value' => variable_get('druais_numberof_group', 1),
     '#description' => t('Define how many group per class')
   );
   
   $form['druais_semesterper_class'] = array(
     '#type' => 'textfield',
     '#size' => 3,
     '#title' => 'How many semester per class period',
     '#default_value' => variable_get('druais_semesterper_class', 2),
     '#description' => t('Define how many semester per class period, default 2 semester per class period')
   );
   
   $form['druais_set_column_fieldset'] = array(
     '#type' => 'fieldset',
     '#title' => t('Set number of column'),
     '#description' => t('When you press "Set Column" button some column in "kurikulum_item" table will be added or removed. So I warn you, when you press this button might be missing some data. <blink><b>DATA LOSS MAY OCCUR WHEN YOU PRESS THIS BUTTON</b></blink>'),
     '#collapsible' => TRUE,
     '#collapsed' => TRUE
   );
   
   $form['druais_set_column_fieldset']['druais_set_column'] = array(
     '#type' => 'submit',
     '#title' => t('Set Column'),
     '#submit' => array('druais_admin_settings_submit')
   );
   
   return system_settings_form($form);
 }
 
 /**
  * Validate the druais configuration form.
  */
 function druais_admin_settings_validate($form, $form_state) {
   $numberof_class = $form_state['values']['druais_numberof_class'];
   $numberof_group = $form_state['values']['druais_numberof_group'];
   $semesterper_class = $form_state['values']['druais_semesterper_class'];
   
   if (!is_numeric($numberof_class) || !is_numeric($numberof_group) || !is_numeric($semesterper_class)) {
     form_set_error('druais', t('Please enter a number'));
   }
 }
 
 /**
  * Handle post-validation form submission
  */
 function druais_admin_settings_submit($form, $form_state) {
   $class = (int) variable_get('druais_numberof_class', $form_state['values']['druais_numberof_class']);
   $semester = (int) variable_get('druais_semesterper_class', $form_state['values']['druais_semesterper_class']);
   $total_semester = $semester * $class;
   $total_semester_saved = (int) variable_get('druais_total_semester', 0);
   $total_difference = $total_semester_saved - $total_semester;
   
   $sql = "select * from (SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS where table_name like 'kurikulum_item') c where c.column_name like 'durasi_semester_%'";
   echo $sql;
   $qresult = db_query($sql);
   $last_value = $qresult->num_rows;
   
   switch ($total_difference) {
     case ($total_difference < 0):
       /**
        * kalau hasilnya minus berarti kolom di database kurang berarti harus ditambah sejumlah selisihnya
        * bila selisinya minus jadikan plus
        * ambil nilai terakhir di database
        * ulangi selisih-kali dari nilai terakhir penambahan kolom ke database.
        */
       $total_difference = $total_difference * -1; //jadikan plus
       echo 'less than 0';
       while ($total_difference != 0) {
         $sql = sprintf("alter table kurikulum_item add durasi_semester_%d int unsigned not null default 0",++$last_value);
         if (db_query($sql)) {
           $total_difference--;
         }
       }
       variable_set('druais_total_semester',$total_semester);
       break;
     case ($total_difference > 0):
       /**
        * kalau hasilnya plus berarti kolom di database berlebih, dan harus dikurang sebanyak jumlah selisih
        * ambil nilai terakhir dari database
        * ulangi selisih kali dari nilai terakhir mengurangi kolom di database.
        */
       echo 'more than 0';
       while ($total_difference != 0) {
         $sql = sprintf("alter table kurikulum_item drop column durasi_semester_%d",$last_value--);
         if (db_query($sql)) {
           $total_difference--;
         }
       }
       variable_set('druais_total_semester',$total_semester);
       break;
     case ($total_difference = 0):
       // bila hasilnya 0 maka tabel di database tidak berubah;
       echo 'equal than 0';
       var_dump($sql);
       drupal_set_message(t('Setting has not changed and the database is not touched','status'));
       break;
     default:
       echo 'default than 0';
       drupal_set_message(t('Do nothing'), 'status');
       echo $total_difference;
   }
 }